import React from 'react';
import {HStack, Image, Pressable, Text} from "@gluestack-ui/themed";
import {useStore} from "@/store";
import useTool from "@/utils";
import {checkAccount} from "@/services/user";

const ChannelSwitch = ({onChange}: { onChange?: (c: number) => void }) => {
    const channel = useStore.use.channel();
    const updateUserInfo = useStore.use.updateUserInfo();
    const changeChannel = useStore.use.changeChannel();
    const {getChannel} = useTool();

    function handleChange() {
        let c = channel == 1 ? 2 : 1;
        checkAccount({
            channel: c
        }).then((res) => {
            console.log("切换平台",res)
            updateUserInfo(res.data);
            changeChannel(c);
            onChange && onChange(c);
        });

    }

    return (
        <Pressable onPress={handleChange}>
            <HStack
                mr={"$3"}
                mb={"$1"}
                justifyContent={"flex-end"}
                alignItems={"center"}
                space={"xs"}
            >
                <Text
                    color="#383838"
                    size={"xs"}
                >当前平台:{getChannel(channel)}</Text>
                <Image w={12} h={12} source={require('@/theme/assets/images/icon/switch.png')}
                       alt={"当前平台"}></Image>
            </HStack>
        </Pressable>
    );
};

export default ChannelSwitch;